#include <dbg.h>

#include <cassert>
#include <numeric>
using namespace std;

class Solution {
public:
    int maxScore(vector<int>& cardPoints, int k) {
        int sum = accumulate(cardPoints.begin(), cardPoints.begin() + k, 0);
        int res = sum;
        for (int i = 0; i < k; ++i) {
            sum = sum - cardPoints[k - i - 1] +
                  cardPoints[cardPoints.size() - i - 1];
            res = max(res, sum);
        }
        return res;
    }
};

int main() {
    Solution s;

    vector<int> nums1 = {1, 2, 3, 4, 5, 6, 1};
    vector<int> nums2 = {2, 2, 2};
    vector<int> nums3 = {9, 7, 7, 9, 7, 7, 9};

    int k1 = 3;
    int k2 = 2;
    int k3 = 7;

    int res1 = 12;
    int res2 = 4;
    int res3 = 55;

    assert(s.maxScore(nums1, k1) == res1);
    assert(s.maxScore(nums2, k2) == res2);
    assert(s.maxScore(nums3, k3) == res3);

    return 0;
}